package com.qf.ssm.service.impl;

import com.qf.ssm.ex.ServiceException;
import com.qf.ssm.mapper.PickUpInformationMapper;
import com.qf.ssm.po.DepositClearing;
import com.qf.ssm.po.Emp;
import com.qf.ssm.po.OrderClearing;
import com.qf.ssm.po.PickUpInformation;
import com.qf.ssm.qo.OrderStatusQO;
import com.qf.ssm.qo.PickUpInformationQo;
import com.qf.ssm.result.ResultStatus;
import com.qf.ssm.service.CarOrderService;
import com.qf.ssm.service.PickUpInformationService;
import com.qf.ssm.utlis.CommonBeanutils;
import com.qf.ssm.vo.PickUpInformationVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author 黄梓濠
 * @Create 2024-01-04-17:15
 */
@Service
@Transactional
public class PickUpInformationServiceImpl implements PickUpInformationService {
    @Autowired
    private PickUpInformationMapper pickUpInformationMapper;
    @Autowired
    private CarOrderService carOrderService;
    //查询原有的取车订单数据
    @Override
    public PickUpInformationVo getPickUpInfor(Integer orderId) {
        //判断是否存在原始数据没有则添加
        Integer count1 = 0;
        try {
            count1 = pickUpInformationMapper.selectPickUpInByOrderId(orderId);
        } catch (ServiceException e) {
            throw new ServiceException(ResultStatus.SYS_ERROR);
        }
        if (count1 == 0){
            //添加order表的信息
            Integer count = null;
            try {
                count = pickUpInformationMapper.insertOrderpickUpInformation(orderId);
            } catch (ServiceException e) {
                throw new ServiceException(ResultStatus.SQL_INSERT_ERROR);
            }
            if (count < 1){
                throw  new ServiceException(ResultStatus.SQL_INSERT_ERROR);
            }
        }

        PickUpInformation pickUpInformation = null;
        List<OrderClearing> orderClearings = null;
        List<DepositClearing> depositClearings = null;
        List<Emp> emps = null;

        PickUpInformationVo pickUpInformationVo = null;
        try {
            //查询出原有的数据
            pickUpInformation = pickUpInformationMapper.selectpickUpInformation(orderId);
            //po转换为bo
            pickUpInformationVo = CommonBeanutils.convertTo(pickUpInformation, PickUpInformationVo::new);
            //查询全部订单支付方式
            orderClearings = pickUpInformationMapper.selectAllOrderClearing();
            //查询全部的押金支付方式
            depositClearings = pickUpInformationMapper.selectAllDepositClearing();
            //查询全部的员工信息
            emps = pickUpInformationMapper.selectAllEmp();
        } catch (ServiceException e) {
            throw new ServiceException(ResultStatus.GET_DATA_ERROR);
        }
        //设置数据
        pickUpInformationVo.setOrderClearing(orderClearings);
        pickUpInformationVo.setDepositClearing(depositClearings);
        pickUpInformationVo.setEmp(emps);
        return pickUpInformationVo;
    }

    //添加所有的取车订单信息
    @Override
    public Integer pickUpInformationFinsh(PickUpInformationQo pickUpInformationQo) {
        Integer count = null;
        try {
            count = pickUpInformationMapper.updatePickUpInformation(pickUpInformationQo);
        } catch (ServiceException e) {
            throw new ServiceException(ResultStatus.SQL_INSERT_ERROR);
        }
        if (count < 1){
            throw  new ServiceException(ResultStatus.SQL_INSERT_ERROR);
        }
        //把订单状态设置为待还车
        OrderStatusQO orderStatusQO = new OrderStatusQO();
        Integer orderId = pickUpInformationQo.getOrderId();
        orderStatusQO.setOrderId(new Integer[]{orderId});
        orderStatusQO.setOrderStatusId(2);
        Integer count1 = null;
        try {
             count1 = carOrderService.editOrderStatus(orderStatusQO);
        } catch (Exception e) {
            throw new ServiceException(ResultStatus.STATUS_UPDATE_ERROR);
        }
        if (count1 < 1){
            throw new ServiceException(ResultStatus.STATUS_UPDATE_ERROR);
        }
        return count;
    }
}
